<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Merchants extends CI_Controller {

	public $data = array();

	function __construct()
	{
		parent::__construct();

		if( !$this->session->userdata('logged_in') )
			redirect('login');

		$this->load->model('merchant_model');
		$this->load->config('regions');
	}

	function index()
	{

		$this->load->library('pagination');

		$offset = $this->uri->segment(2, 0);
		$limit = 2;
		
		$this->data = $this->merchant_model->get_all(array(), $limit, $offset);

		$config['base_url'] = site_url('merchants');
		$config['total_rows'] = $this->data['count'];
		$config['per_page'] = $limit; 

		$this->pagination->initialize($config);

		$this->layout->view('merchants/list', $this->data);
		
	}

	function add( $id = 0 )
	{
		$rules = array();
		$rules[] = 	array('field' => 'name',    'label' => 'Name',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'merchant_id',    'label' => 'Merchant ID',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'marketplace_id',    'label' => 'Marketplace ID',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'access_key',    'label' => 'Access Key',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'secret_key',    'label' => 'Secret Key',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'region',    'label' => 'Region',    'rules' => 'trim|required|xss_clean');
		$rules[] = 	array('field' => 'reports_enabled',    'label' => 'Reports Enabled',    'rules' => 'trim|required|xss_clean');


		$this->load->library('form_validation');
		$this->form_validation->set_rules($rules);

        $error = '';

		if ($this->form_validation->run())
		{
			$form = $this->security->xss_clean($_POST);           
           

			$insert_data = array();
			$insert_data['name'] 			= $form['name'];
			$insert_data['merchant_id'] 	= $form['merchant_id'];
			$insert_data['marketplace_id'] 	= $form['marketplace_id'];
			$insert_data['access_key'] 		= $form['access_key'];
			$insert_data['secret_key'] 		= $form['secret_key'];
			$insert_data['region'] 			= $form['region'];
			$insert_data['reports_enabled'] = $form['reports_enabled'];

			if(!$id)
			{
				$this->db->insert('merchants', $insert_data);
				$success_message = 'Record Added Successfully!.'; 
			}
			else
			{
				$this->db->where('id', $id);
				$this->db->update('merchants', $insert_data); 

				$success_message = 'Record Updated Successfully!.'; 
			}

			$this->update_key($insert_data['merchant_id']);
			
			$this->session->set_flashdata('success_message', $success_message);
			redirect('merchants');

		}

		$edit_data = array(	'id' => '0',
							'name' => '', 
							'merchant_id' => '', 
							'marketplace_id' => '',
							'access_key' => '',
							'secret_key' => '',
							'region' => 'US',
							'reports_enabled' => 'y'
							);
		if($id)
		{
			$edit_data = $this->merchant_model->get_by_id($id);
		}

		$this->data['edit_data'] 	= $edit_data;
		$this->data['error'] 		= $error;
		$this->data['regions'] 		= $this->config->item('regions');
		
		$this->layout->view('merchants/add', $this->data );
	}

	function update_key( $merchant_id = '' )
	{

		$row = $this->merchant_model->get_by_merchant_id($merchant_id);

		if(count($row))
		{
			$this->merchant_model->insert_key($merchant_id);
		}

	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */